/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_ACELITE_MOCK_VSYNC_THREAD_H
#define OHOS_ACELITE_MOCK_VSYNC_THREAD_H

#include <QMutex>
#include <QThread>

#include "product_adapter.h"

namespace OHOS {
namespace ACELite {
/**
 * @brief The VsyncThread class is a very simple mock implementation for display driver on real device, it just try
 * continuing to dispatch one vsync message into the APP's event loop every 16ms, which can trigger the APP do the
 * UI render work, and which makes the UI shown at last.
 *
 * NOTE: This mock implementation just for simulation purpose, it might not be suitable for real device.
 */
class VsyncThread : public QThread {
public:
    VsyncThread() = default;
    virtual ~VsyncThread() = default;
    void run() override;
    void Quit();
    bool IsRendring();
    void RenderEndNotifer();

private:
    bool CheckRenderingState();
    void SimuOneTickBreak() const;
    TEDispatchingResult SimuOneTickEvent();
    const uint32_t MAX_STUCK_COUNT = 100;
    const uint32_t MAX_MISSING_COUNT = 30; // trace out if no TE event is dispatching out for 30 ticks
    uint32_t missingCount_ = 0;
    uint32_t renderTickCount_ = 0;
    uint32_t stuckCount_ = 0;
    QMutex renderLock;
    volatile bool taskQuitFlag = false;
};
} // namespace ACELite
} // namespace OHOS
#endif // OHOS_ACELITE_MOCK_VSYNC_THREAD_H
